package com.codegeek.project.slope.service.impl;

import com.codegeek.common.utils.DateUtils;
import com.codegeek.project.slope.domain.SlopeAlarm;
import com.codegeek.project.slope.mapper.SlopeAlarmMapper;
import com.codegeek.project.slope.service.ISlopeAlarmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 告警记录Service业务层处理
 *
 * @author codegeek
 * @date 2020-02-17
 */
@Service
public class SlopeAlarmServiceImpl implements ISlopeAlarmService {
    @Autowired
    private SlopeAlarmMapper slopeAlarmMapper;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 查询告警记录
     *
     * @param alarmId 告警记录ID
     * @return 告警记录
     */
    @Override
    public SlopeAlarm selectSlopeAlarmById(Long alarmId) {
        return slopeAlarmMapper.selectSlopeAlarmById(alarmId);
    }

    /**
     * 查询告警记录列表
     *
     * @param slopeAlarm 告警记录
     * @return 告警记录
     */
    @Override
    public List<SlopeAlarm> selectSlopeAlarmList(SlopeAlarm slopeAlarm) {
        return slopeAlarmMapper.selectSlopeAlarmList(slopeAlarm);
    }

    /**
     * 新增告警记录
     *
     * @param slopeAlarm 告警记录
     * @return 结果
     */
    @Override
    public int insertSlopeAlarm(SlopeAlarm slopeAlarm) {
        slopeAlarm.setCreateTime(DateUtils.getNowDate());
        return slopeAlarmMapper.insertSlopeAlarm(slopeAlarm);
    }

    /**
     * 修改告警记录
     *
     * @param slopeAlarm 告警记录
     * @return 结果
     */
    @Override
    public int updateSlopeAlarm(SlopeAlarm slopeAlarm) {
        slopeAlarm.setUpdateTime(DateUtils.getNowDate());
        return slopeAlarmMapper.updateSlopeAlarm(slopeAlarm);
    }

    /**
     * 批量删除告警记录
     *
     * @param alarmIds 需要删除的告警记录ID
     * @return 结果
     */
    @Override
    public int deleteSlopeAlarmByIds(Long[] alarmIds) {
        return slopeAlarmMapper.deleteSlopeAlarmByIds(alarmIds);
    }

    /**
     * 删除告警记录信息
     *
     * @param alarmId 告警记录ID
     * @return 结果
     */
    @Override
    public int deleteSlopeAlarmById(Long alarmId) {
        return slopeAlarmMapper.deleteSlopeAlarmById(alarmId);
    }

    /**
     * 首页预警分类统计
     */
    @Override
    public List<Map<String, Object>> countSlopeAlarmLevel() {
        StringBuilder sql = new StringBuilder(" select count(*) num,alarm_level alarmLevel from slope_alarm WHERE alarm_state= 0 group by alarm_level");
        return jdbcTemplate.queryForList(sql.toString());
    }

    @Override
    public int changeAlarmStatusById(Long alarmId, Integer state) {
        StringBuilder sql = new StringBuilder(" UPDATE slope_alarm set alarm_state = '" + state + "' where alarm_id = '" + alarmId + "'");
        return jdbcTemplate.update(sql.toString());
    }

    @Override
    public int changSelectedAlarmStatus(String ids, Integer state) {
        StringBuilder sql = new StringBuilder("UPDATE slope_alarm set alarm_state = '" + state + "' where alarm_id in (" + ids + ")");
        return jdbcTemplate.update(sql.toString());
    }

    @Override
    public List<SlopeAlarm> selectAlarmListByLevel(Integer alarmLevel) {
        return slopeAlarmMapper.selectAlarmListByLevel(alarmLevel);
    }

    @Override
    public int oneClickClose() {
        StringBuilder sql = new StringBuilder("UPDATE slope_alarm AS A INNER JOIN (SELECT alarm_id FROM slope_alarm WHERE alarm_state = 0) AS B ON A.alarm_id=B.alarm_id SET A.alarm_state = 2");
        return jdbcTemplate.update(sql.toString());
    }
}
